<template>
    <div class="salesagentForm">
      <!-- element组件库的Dialog对话框默认可以通过点击 modal 关闭 Dialog，即点击空白处弹框可关闭。 -->
      <!-- :visible.sync 子组键改变父组件的值 控制子组件的显示-->
      <el-dialog title="修改销售代理" :visible.sync="editDialogIsShow" :before-close="handleClose" :close-on-click-modal="false">
        <div style="position: absolute;top:83px;left:225px;" v-show="oldImg">
          <img :src="getImgUrl(this.salesagent.ssaimg)" style="width: 146px;height: 146px;border-radius: 4px"/>
        </div>
        <el-form   label-width="120px"  ref="addSalesagentUploadForm">
          <!-- 代码中的1表示第几个 和引入js的type变量对应 -->
          <el-form-item label="头像">
            <el-upload
              class="upload"
              :action="getUploadUrl"
              list-type="picture-card"
              :limit="1"
              accept="image/*"
              :on-change="(file,fileList)=>{imgHandleChange(file,fileList,1,this,'addSalesagentUploadForm','Salesagent')}"
              :on-remove="(file,fileList)=>{imgHandleRemove(1,this)}"
              :on-before="(file,fileList)=>{imghandleBeforeUpload(file,1,this)}"
              :auto-upload="false"
              :file-list="fileList[0]"
              ref="file1">
              <i class="el-icon-plus"></i>
            </el-upload>
            <!--<viewer-dialog ref="viewer" :images="imagesUp"></viewer-dialog>-->
          </el-form-item>
        </el-form>
      <el-form :model="salesagent" label-width="80px"  :rules="editSalesagentRules" ref="editSalesagentForm">
        <el-form-item label="姓名" prop="saname">
          <el-col :span="16">
            <el-input v-model="salesagent.saname" placeholder="请填入姓名" clearable/>
          </el-col>
        </el-form-item>
        <el-form-item  label="性别">
          <el-col :span="16">
            <el-radio label="男" v-model="salesagent.sasex">男</el-radio><el-radio label="女" v-model="salesagent.sasex">女</el-radio>
          </el-col>
        </el-form-item>
        <el-form-item label="电话" prop="saphone">
          <el-col :span="16">
            <el-input v-model="salesagent.saphone" placeholder="请填入手机号" clearable/>
          </el-col>
        </el-form-item>
        <el-form-item label="账号" prop="said">
          <el-col :span="16">
            <el-input v-model="salesagent.said" placeholder="请填入账号" clearable/>
          </el-col>
        </el-form-item>
        <el-form-item label="密码" prop="sapwd">
          <el-col :span="16">
            <el-input v-model="salesagent.sapwd" placeholder="请填入密码" clearable/>
          </el-col>
        </el-form-item>
        <el-form-item label="个人描述" prop="sadescribe">
          <el-col :span="16">
            <el-input type="textarea" :rows="5" v-model="salesagent.sadescribe" placeholder="请输入个人描述" />
          </el-col>
        </el-form-item>
      </el-form>
      <el-row>
         <el-col :span="12">
         <span>&nbsp;</span></el-col>
        <el-col :span="6">
           <span><el-button type="primary" @click="editSalesagent('editSalesagentForm')">修改</el-button></span>
        </el-col>

    </el-row>
      </el-dialog>
    </div>
</template>

<script>
  import api from "../../../../axios/api"
  import rules from "../../../../static/js/validator/rules.js"
  import utils from "../../../../static/js/utils/upload";
    var initfavOption = [];
    export default {
        name: "editSalesagent",
        data(){
          return {
            salesagent:{},
              checkAll: false,
              isIndeterminate: true,
              editSalesagentRules:{saname:[{required:true,message:"姓名不能为空",trigger:'blur'}],
                saphone:[{required:true,message:"手机号不能为空",trigger:'blur'},{trigger:'change',validator:rules.FormValidate.Form().validatePhone}],
                said:[{required:true,message:"账号不能为空",trigger:'blur'},{trigger:'change',validator:rules.FormValidate.Form().validateCode}],
                sapwd:[{required:true,message:"密码不能为空",trigger:'blur'},{trigger:'change',validator:rules.FormValidate.Form().validatePsdReg}]},
              editDialogIsShow:false,
            oldImg: false,
            //上传使用
            fileList: [[],[],[]], //缓存区文件
            uploadFile:[[],[],[]], //  上传用文件
            formData:{files:[]},
            imagesUp:null,
            //上传使用结束
          }
        },
      methods: {
        init(salesagent) {
          this.fileList = [[],[],[]] //缓存区文件
          this.uploadFile = [[],[],[]],
          this.editDialogIsShow = true;
          this.salesagent = salesagent;
          console.log(salesagent);
          if(this.salesagent.saimg!=null&&this.salesagent.saimg!=''){
            this.oldImg = true;
          }
        },
        handleCheckAllChange(val) {
          this.salesagent.favs = val ? initfavOption : [];
          this.isIndeterminate = false;
        },
        handleCheckedFavsChange(value) {
          let checkedCount = value.length;
          this.checkAll = checkedCount === this.favs.length;
          this.isIndeterminate = checkedCount > 0 && checkedCount < this.favs.length;
        },
        //关闭页面
        handleClose() {
          this.editDialogIsShow = false;
          //给父组件传参
          this.$emit("editDialogIsShow", this.editDialogIsShow);
        },
        editSalesagent:function(editSalesagentForm){
          this.$refs[editSalesagentForm].validate(valid => {//此处refs对象和标签上的ref对应
            if (valid) {
              //如果通过验证 to do...
              console.log(this.salesagent);
              this.setDataFromAxios();
              alert(this.salesagent.sano+"修改成功");
              this.handleClose();
            } else {
              console.log('error submit!!')
              return false
            }
          });

        },
        //关闭页面
        handleClose() {
          this.editDialogIsShow = false;
          //给父组件传参
          this.$emit("editDialogIsShow", this.editDialogIsShow);
        },
        setDataFromAxios:function(){
          //为什么要封装一下方法 1.每个vue组件有自己的一些参数处理，简化调用本来这个setDataFromAxios这个方法属于api 重新封装就属于当前页面
          api.setDataFromAxios("/api/salesagentController/updateSalesagentBySano",this.salesagent,"post")
            .then(res=>{this.leagues = res.leagues});//.then方法 就是当服务器有返回的时候执行回调函数，对比jquery的ajax success:function（data）
        },
        updateView(e) {
          this.$forceUpdate();
        },
        getImgUrl: function (imgsrc) {
          return  this.$store.state.host + "/upload/images/head/" + imgsrc;
        },
        imghandleBeforeUpload(file,type,vm) {

          return utils.UPLOAD.Upload().handleBeforeUpload(file,type,vm);
        },
        imgHandleChange(file, fileList, type,vm,modelName,name) {
          if(vm.fileList[type].length!=0){
            alert("请先删除再提交")
          }
          return utils.UPLOAD.Upload().handleChange(file, fileList, type,vm,modelName,name);
        },
        imgHandleRemove(type,vm) {

          return utils.UPLOAD.Upload().handleRemove( type,vm);
        },
        uploadImg:function(modelName,name){
          //  使用formdata格式
          let formData = new FormData();
          formData.append("model",modelName);//图片上传子目录
          console.log(formData);
          console.log(this.formData.files[name]);
          if (this.formData.files[name]) {
            formData.append("file", this.formData.files[name]);
          }
          console.log("----------------------------");
          console.log(formData);
          //为什么要封装一下方法 1.每个vue组件有自己的一些参数处理，简化调用本来这个setDataFromAxios这个方法属于api 重新封装就属于当前页面
          api.setDataFromAxios(this.$store.state.host+"/uploadImg",formData,"post",{headers: { "Content-Type": "multipart/form-data" }})
            .then(dto=>{
              this.oldImg = false;
              this.$message.info("上传图片成功!");
              if(dto.code==200){
                this.salesagent.saimg = dto.obj;
              }
            });//.then方法 就是当服务器有返回的时候执行回调函数，对比jquery的ajax success:function（data）
        },
      },
      computed:{
        getUploadUrl(){
          return      this.$store.state.host+"/uploadImg";
        }
      },
      //监听
      watch: {
        editDialogIsShow(newValue, oldValue) {//配合:visible.sync监听变化
          this.editDialogIsShow = newValue;
        }
      },
      created() {
        for(var favindex=0;favindex < favOptions.length;favindex++){
          initfavOption.push(favOptions[favindex].name);
        }
      }
    }
</script>
<style scoped lang="stylus">
 .salesagentForm
   margin 0 auto
   width 1000px
   text-align left
</style>
